package sopt.engSearch;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONObject;

import com.ibatis.sqlmap.client.SqlMapClient;

import comm.util.RequestManager;
import comm.util.SoptHashMap;
import comm.util.SqlMapLoader;

import statics.SoptVars;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;

public class TwitterAPI {
	private SqlMapClient sqlMapper = SqlMapLoader.getInstance();
	
	public JSONObject regMember(HttpServletRequest request,HttpServletResponse response) throws ParserConfigurationException, IOException, SQLException{
		JSONObject result = null;
		
		SoptHashMap hm = new SoptHashMap();
		hm = RequestManager.getRequest(request);
		
		
		Twitter twitter = new Twitter();
	    twitter.setOAuthConsumer(SoptVars.twitterConsumerKey, SoptVars.twitterConsumersecret);
	    
		AccessToken at = null;
		try {
			at = twitter.getOAuthAccessToken(hm.get("OAUTH_TOKEN").toString(),request.getSession().getAttribute("TWITTER_TOKEN_SECRET").toString());
		} catch (TwitterException e1) {
			e1.printStackTrace();
			return result;
		}
		
		request.getSession().removeAttribute("TWITTER_TOKEN_SECRET");
		
		twitter.setOAuthAccessToken(at);
		
		System.out.println(at.getToken());
		System.out.println(at.getTokenSecret());
		System.out.println("===========");
		
		User user = null;
		try {
			user = twitter.verifyCredentials();
		} catch (TwitterException e) {
			e.printStackTrace();
			return result;
		}
		
		if(user!=null){
			hm.put("MEMBER_ID", user.getId());
			hm.put("MEMBER_PASSKEY", at.getToken()+"@"+at.getTokenSecret());
			
			request.getSession().setAttribute("MEMBER_PASSKEY", hm.get("MEMBER_PASSKEY"));
			
			hm.put("MEMBER_NICK", user.getScreenName());
			hm.put("MEMBER_URL", "http://twitter.com/" + hm.get("MEMBER_NICK"));
			hm.put("MEMBER_IMG",user.getProfileImageURL().toString());
			
			System.out.println("logger" + this.toString());
			System.out.println(hm);
			System.out.println("logger" + this.toString());
			
			int resultUpdate = sqlMapper.update("member.updateMember", hm);
			
			//기존에 없다면 Insert!
			if(resultUpdate==0)sqlMapper.insert("member.regMember", hm);
			
			result = new JSONObject( ((SoptHashMap)sqlMapper.queryForObject("member.getMemberById",hm.get("MEMBER_ID").toString())) );
		}
		
		
		return result;
		
	}
	
	public JSONObject getMemberInfoBySeq(String memberSeq){
		try {
			return new JSONObject( ((SoptHashMap)sqlMapper.queryForObject("member.getMemberBySeq",memberSeq))) ;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			return null;
		}
	}
	
	public void toAuthPage(HttpServletRequest request,HttpServletResponse response){
		
		Twitter twit = new Twitter();
		twit.setOAuthConsumer(SoptVars.twitterConsumerKey,SoptVars.twitterConsumersecret);

		RequestToken requestToken = null;
		try {
			requestToken = twit.getOAuthRequestToken();
		} catch (TwitterException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		request.getSession().setAttribute("TWITTER_TOKEN_SECRET",requestToken.getTokenSecret());
		
		try {
			response.sendRedirect(requestToken.getAuthorizationURL());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
		
	}
}
